package imp.core.misc;


import imp.core.basics.date.Date;
import imp.core.data.songdata.Song;

import java.util.ArrayList;


/**
 * This class offers methods to sort the array list containing the song objects.
 * @author Patrick Arnold
 * @since 09.09.2010
 *
 */
public class SortManager {

	
	/**
	 * Sorts the array list containing the songs.
	 * @param songs The list containing the song objects.
	 * @param sortKey The key which specifies how to sort the list.
	 * @return The list sorted by the specified key.
   	 */
	public static ArrayList<Song> sortSonglist( ArrayList<Song> songs, int sortKey) {

		String key = "";
		
		if( sortKey == 0) 
			key = "title";
		if( sortKey == 1) 
			key = "interpret";
		if( sortKey == 2) 
			key = "year";
		if( sortKey == 3) 
			key = "rating";
		if( sortKey == 4) 
			key = "length";
		if( sortKey == 5) 
			key = "lastDate";
		if( sortKey == 6) 
			key = "numberOfPlayings";
		if( sortKey == 7) 
			key = "registerDate";
		
		
		if( key.equals( "title") || key.equals( "interpret")) {
			songs = sortByString( songs, key);
		} else {
			songs = sortByNumber( songs, key);
		}
		
		
		return songs;
		
	}
	
	
	
	/**
	 * Sorts the list by strings (by title or interpret).
	 * @param songs The list containing the songs.
	 * @param key The sort key.
	 * @return The sorted list.
	 */
	private static ArrayList<Song> sortByString( ArrayList<Song> songs, String key) {
		
		String a = "", b = "";   // Auxiliary strings
     
     
		// Comparing strings
		if( key.equals( "title") || key.equals( "interpret")) {

     
			for( int i = 0; i <= songs.size() - 1; i++) {
        	
				int swaps = 0;

				for( int j = 0; j <= songs.size() - 2; j++) {


					if( key.equals( "title")) {
						a = songs.get(j).getTitle();
						a = a.toLowerCase();
						b = songs.get(j+1).getTitle();
						b = b.toLowerCase();
					}
					if( key.equals( "interpret")) {
						a = songs.get(j).getInterpret();
						a = a.toLowerCase();
						b = songs.get(j+1).getInterpret();
						b = b.toLowerCase();
					}


					if( a.compareTo(b) >= 0 ) {
						Song tmp = songs.get( j+1);
						songs.set( j+1, songs.get( j));
						songs.set( j, tmp);
						swaps += 1;
					}
				
				}

					
				if( swaps == 0) {   // list is successfully sorted
					break;
				}
					
			}

		}
			
			
		return songs;

	}		
		

		
	/**
	 * Sorts the list by numbers (by year, rating, last date etc.).
	 * @param songs The list containing the songs.
	 * @param key The sort key.
	 * @return The sorted list.
	 */
	private static ArrayList<Song> sortByNumber( ArrayList<Song> songs, String key) {
     
		int a = 0, b = 0;

		
		
		// Comparing numbers
		if( key.equals( "year") || key.equals( "rating") || key.equals( "length")
				|| key.equals( "numberOfPlayings") || key.equals( "lastDate")
				|| key.equals( "registerDate")) {


			for( int i = 0; i <= songs.size() - 1; i++) {

				int swaps = 0;

				for( int j = 0; j <= songs.size() - 2; j++) {

    				if( key.equals( "year")) {
    					a = songs.get(j).getYear();
    					b = songs.get(j+1).getYear();
    				}
    				if( key.equals( "rating")) {
    					a = (int) (songs.get(j).getRating() * 10);
    					b = (int) (songs.get(j+1).getRating() * 10);
    				}
    				if( key.equals( "numberOfPlayings")) {
    					a = songs.get(j).getNumberOfPlayings();
    					b = songs.get(j+1).getNumberOfPlayings();
    				}
    				if( key.equals( "length")) {
    					a = songs.get(j).getLength();
    					b = songs.get(j+1).getLength();
    				}
    				if( key.equals( "lastData")) {      // Special case
    					Date last = songs.get(j).getLastPlayed();
    					a = last.getDateKey();
    					last = songs.get(j+1).getLastPlayed();
    					b = last.getDateKey();
    				}
    				if( key.equals( "registerDate")) {      // Special case
    					Date regDate = songs.get(j).getRegistrationDate();
    					a = regDate.getDateKey();
    					regDate = songs.get(j+1).getRegistrationDate();
    					b = regDate.getDateKey();
    				}
    
    				
    				if( b > a) {
    					Song tmp = songs.get( j+1);
    					songs.set( j+1, songs.get( j));
    					songs.set( j, tmp);
    					swaps += 1;
    				}
              
				}

			
				if( swaps == 0) {   // songs are successfully sorted
					break;
				}

			}

		}
     
     
		return songs;

	}
  
	
	
	/**
	 * Sorts an array list lexicographically.
	 * @param list The array list which is to be sorted
	 * @return The sorted array list
	 */
	public static ArrayList<String> sortArraylist( ArrayList<String> list) {
		
		String a = "", b = "";   // Auxiliary strings

     
		for( int i = 0; i <= list.size() - 1; i++) {
        	
			int swaps = 0;
			
			for( int j = 0; j <= list.size() - 2; j++) {

				a = list.get(j);
				a = a.toLowerCase();
				b = list.get(j+1);
				b = b.toLowerCase();


				if( a.compareTo(b) >= 0 ) {
					String tmp = list.get( j+1);
					list.set( j+1, list.get( j));
					list.set( j, tmp);
					swaps += 1;
				}
				
			}
			
			
			if( swaps == 0) {   // list is successfully sorted
				break;
			}
					
		}
			
			
		return list;
		
	}
	
 
}